package com.vic.kjlink.order.third;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vic.kjlink.order.model.User;

import pers.vic.boot.base.craw.BaseCrawl;
import pers.vic.boot.util.JsonUtil;

/**
 * @description: 登录到OA
 * @author Vic.xu
 * @date: 2021/8/20 0020 8:14
 */
public class OaLoginUtil extends BaseCrawl {
    private static Logger logger = LoggerFactory.getLogger(OaLoginUtil.class);

    // http://oa.kjlink.com/KjlinkPmsBackend/tbRsUser/login?userCode=201088&password=654321
    private static String LOGIN_URL = "http://oa.kjlink.com/KjlinkPmsBackend/tbRsUser/login";

    public static User login(String userCode, String password) {
        OaLoginUtil util = new OaLoginUtil();
        try {
            String text = util.con(LOGIN_URL).data("userCode", userCode).data("password", password).getBodyText();
            logger.info("OA 登录返回结果：\n【{}】", text);
            OaResponse oaResponse = JsonUtil.jsonToObject(text, OaResponse.class);
            if (oaResponse.getCode() == 0 && oaResponse.getUser() != null) {
                OaUser OaUser = oaResponse.getUser();
                User user = new User(OaUser);
                return user;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected int getTimeout() {
        return 30000;
    }

    public static void main(String[] args) {
        User login = login("201088", "654321");
        JsonUtil.printJson(login);
    }
}
